package com.yungu.swift.admin.route.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.order.model.dto.OrderComplainDto;
import com.yungu.swift.order.model.dto.OrderSecretNoReportDto;
import com.yungu.swift.order.model.param.AdminOrderComplainPageParam;
import com.yungu.swift.order.model.param.AdminOrderComplainParam;
import com.yungu.swift.order.model.param.AdminOrderUuidPageParam;
import com.yungu.swift.order.service.OrderComplainService;
import com.yungu.swift.order.service.OrderSecretNoReportService;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;

/**
 * @author : cuixiuyin
 * @date : 2019/7/19
 */
@RestController
@RequestMapping("/admin/v1/order/complain")
@Api(value = "【订单管理】", tags = "订单投诉管理")
public class OrderComplainAdmin {

    @Reference
    private OrderComplainService orderComplainService;
    @Reference
    private OrderSecretNoReportService orderSecretNoReportService;

    @ResponseImgOverride
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "订单投诉列表", httpMethod = "POST")
    public ResponseData<PageVo<OrderComplainDto>> queryPage(@RequestBody AdminOrderComplainPageParam complainPageParam) {
        return orderComplainService.queryPage(complainPageParam);
    }

    @RequestMapping(value = "/call/report/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "通话记录列表", httpMethod = "POST")
    public ResponseData<PageVo<OrderSecretNoReportDto>> callReportQueryPage(@RequestBody AdminOrderUuidPageParam pageParam) {
        if (ParamUtil.isIllegal(pageParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderSecretNoReportService.queryPage(pageParam);
    }

    @RequestMapping(value = "/tripRecording/get", method = RequestMethod.GET)
    @ApiOperation(value = "订单投诉行程录音文件获取", httpMethod = "GET")
    public ResponseData<String> callReportQueryPage(@RequestParam(value = "orderUuid") String orderUuid) {
        if (ParamUtil.isIllegal(orderUuid)) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderComplainService.getTripRecording(orderUuid);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "订单投诉详情", httpMethod = "GET")
    public ResponseData<OrderComplainDto> detail(@RequestParam String uuid) {
        return orderComplainService.get(uuid);
    }

    @RequestMapping(value = "/handle", method = RequestMethod.POST)
    @ApiOperation(value = "订单投诉处理", httpMethod = "POST")
    public ResponseData<Boolean> handle(@RequestBody AdminOrderComplainParam complainParam) {
        if (ParamUtil.isIllegal(complainParam.getUuid()) || OrderComplainDto.STATUS_PENDING == complainParam.getStatus()) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return this.orderComplainService.handle(complainParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "订单投诉列表导出", httpMethod = "POST")
    public void exportDetailData(@RequestBody AdminOrderComplainPageParam complainPageParam, HttpServletResponse response) {
        try (OutputStream os = response.getOutputStream()) {
            List<OrderComplainDto> beans = orderComplainService.list(MapUtils.convertObjToMap(complainPageParam)).getData();

            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((50 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(9, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(10, ((50 + 1) * 7 + 5) / 7 * 256);
            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("订单编号");
            row.createCell(2).setCellValue("投诉时间");
            row.createCell(3).setCellValue("投诉内容");
            //乘客被投诉
            if (complainPageParam.getRespondentIdentity() == MagicConstant.INT_ONE) {
                row.createCell(4).setCellValue("投诉司机");
                row.createCell(5).setCellValue("投诉司机电话");
            }
            //司机被投诉
            if (complainPageParam.getRespondentIdentity() == MagicConstant.INT_TWO) {
                row.createCell(4).setCellValue("投诉乘客");
                row.createCell(5).setCellValue("投诉乘客电话");
            }
            row.createCell(6).setCellValue("被投诉人");
            row.createCell(7).setCellValue("被投诉人电话");
            row.createCell(8).setCellValue("状态");
            row.createCell(9).setCellValue("处理账户");
            row.createCell(10).setCellValue("备注");

            CellStyle cellStyle = workbook.createCellStyle();
            //设置自动换行
            cellStyle.setWrapText(true);

            for (int rowNum = 1; rowNum <= beans.size(); rowNum++) {
                Row dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                OrderComplainDto bean = beans.get(rowNum - 1);
                for (int cellNum = 1; cellNum <= MagicConstant.INT_TEN; cellNum++) {
                    Cell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            cell.setCellValue(bean.getOrderNo());
                            break;
                        }
                        case 2: {
                            cell.setCellValue(DateUtils.format(bean.getCreateOn()));
                            break;
                        }
                        case 3: {
                            if (bean.getContents() != null) {
                                cell.setCellStyle(cellStyle);
                                cell.setCellValue(bean.getContents());
                            }
                            break;
                        }
                        case 4: {
                            if (bean.getComplainantName() != null) {
                                cell.setCellValue(bean.getComplainantName());
                            }
                            break;
                        }
                        case 5: {
                            cell.setCellValue(bean.getComplainantMobile());
                            break;
                        }
                        case 6: {
                            if (bean.getRespondentName() != null) {
                                cell.setCellValue(bean.getRespondentName());
                            }
                            break;
                        }
                        case 7: {
                            cell.setCellValue(bean.getRespondentMobile());
                            break;
                        }
                        case 8: {
                            if (bean.getStatus() == OrderComplainDto.STATUS_PENDING) {
                                cell.setCellValue("待处理");
                            } else if (bean.getStatus() == OrderComplainDto.STATUS_HANDLED) {
                                cell.setCellValue("已处理");
                            } else if (bean.getStatus() == OrderComplainDto.STATUS_HANDLING) {
                                cell.setCellValue("处理中");
                            } else {
                                cell.setCellValue("");
                            }
                            break;
                        }
                        case 9: {
                            if (bean.getHandlerAccount() != null) {
                                cell.setCellValue(bean.getHandlerAccount());
                            }
                            break;
                        }
                        case 10: {
                            if (bean.getResult() != null) {
                                cell.setCellStyle(cellStyle);
                                cell.setCellValue(bean.getResult());
                            }
                            break;
                        }
                        default:
                            break;
                    }
                }
            }
            response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("orderComplain.xlsx", "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
